import { createRouter, createWebHistory } from "vue-router";
import Login from "../components/Login.vue";
import Home from "../components/Home.vue";
import welcome from "../components/view/welcome.vue";
import User from "../components/view/User.vue";
import Calones from "../components/view/Calones.vue";
import Food from "../components/view/Food.vue";
import Goods from "../components/view/Goods.vue";
import Introduction from "../components/view/Introduction.vue";
import Rights from "../components/view/Rights.vue";
import Sport from "../components/view/Sport.vue";
import ProductDetail from '../components/view/ProductDetail.vue'
const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: "/Home",
    component: Home,
    redirect:"/welcome",
    children: [
      {
        path: "/welcome",
        component: welcome,
      },
      {
        path: "/user",
        component: User,
        
      },
      {
        path: "/rights",
        component:Rights,
        
      },
      {
        path: "/sport",
        component: Sport,
        
      },
      {
        path: "/goods",
        component: Goods,
        
      },
      {
        path: "/introduction",
        component:Introduction,
        
      },
      {
        path: "/calones",
        component: Calones,
        
      },
      {
        path: "/food",
        component: Food,
        
      },
      {
        path: '/product/:id',
        name: 'ProductDetail',
        component: ProductDetail,
        props: true
      }
    ],
  },
  // 添加更多路由
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    return next();
  }
  const user = window.sessionStorage.getItem("user");
  if (!user) {
    return next("/login");
  }
  next();
});

export default router;
